package org.hxyjs.service.impl;


import org.hxyjs.dao.PressDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Account;
import org.hxyjs.entity.Press;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.AccountService;
import org.hxyjs.service.PressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("pressService")
public class PressServiceImpl implements PressService, AccountService {
    @Resource
    PressDao pressDao;
    @Override
    public DTO<Press> getAllPresss()  throws Exception{
        List<Press> presss = pressDao.getAllPresss();
        MessageEnum messageEnum  = Optional.ofNullable(presss).isPresent()?MessageEnum.GETPRESSSUCCESS: MessageEnum.GETPRESSFAIL;
        return new DTO<Press>(messageEnum.getCode(),messageEnum.getMessage(),presss);
    }

    @Override
    public DTO<Press> deletePressByPrno(String prno)  throws Exception{
        Integer result = pressDao.deletePressByPrno(prno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEPRESSSUCCESS:MessageEnum.DELETEPRESSFAIL;
        return new DTO<Press>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Press> updatePressByPrno(Press press)  throws Exception{
        Integer result = pressDao.updatePressByPrno(press);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEPRESSSUCCESS:MessageEnum.UPDATEPRESSFAIL;
        return new DTO<Press>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Press> addPress(Press press) throws Exception {
        Integer result = pressDao.addPress(press);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDPRESSSUCCESS:MessageEnum.ADDPRESSFAIL;
        return new DTO<Press>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Press> getPressByPage(PageInfo pageInfo) throws Exception{
        List<Press> presss = pressDao.getPressByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(presss).isPresent()?MessageEnum.GETPRESSSUCCESS: MessageEnum.GETPRESSFAIL;
        return new DTO<Press>(messageEnum.getCode(),messageEnum.getMessage(),presss,pageInfo);
    }

    @Override
    public DTO<Press> selectPressWithSalesagentAndBooks(String prno) throws Exception {
        Press press = pressDao.selectPressWithSalesagentAndBooks(prno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(prno).isPresent()?MessageEnum.GETPRESSSUCCESS:MessageEnum.GETPRESSFAIL;
        return new DTO<Press>(messageEnum.getCode(),messageEnum.getMessage(),press);
    }

    @Override
    public DTO<Account> getAccountByAccountName(String accountName) throws Exception {
        Press presss = pressDao.getAccountByAccountName(accountName);
        MessageEnum messageEnum  = Optional.ofNullable(accountName).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
        return new DTO<Account>(messageEnum.getCode(),messageEnum.getMessage(),presss);
    }
}
